﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ecdl.demo.model.expression;
using DocumentFormat.OpenXml.Packaging;

namespace ecdl.demo.model.word {
    public class WordContext : Expression {

        public WordContext() {
        }

        public String FileName { get; set; }
        public Expression Expression { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override ExpressionState Evaluate(ExpressionContext context) {

            
            try {
                using (WordprocessingDocument document = WordprocessingDocument.Open(FileName, false)) {

                    return Expression.Evaluate(new ExpressionContext(document));
                
                }
            } catch (ArgumentNullException e) {
                return new ErrorexpressionState(this, e);
            } catch (OpenXmlPackageException e) {
                return new ErrorexpressionState(this, e);
            }
        }
    }
}
